import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public abstract class AbstractPayment
{
   private Membership _membership;
   protected CustomerAccount _customer;
   private double totalPayment;
   private PaymentType paymentType;
   private static double taxRate = .0975;
   
   public AbstractPayment(CustomerAccount customer)
   {
      _customer = customer;
      _membership = _customer.getMembership();
      totalPayment = 0.0;
   }
   
   public void processPayment()
   {
      ArrayList<Item> shoppingCart = _customer.getShoppingCart();
      Iterator<Item> iterator = shoppingCart.iterator(); 
      
      while(iterator.hasNext())
      {
         Item item = (Item) iterator.next();
         totalPayment += getPayment(item);
      }
      
      addTax();
      choosePaymentType();
      makePayment();
      
      while(iterator.hasNext())
      {
         Item item = (Item) iterator.next();
         totalPayment += getPayment(item);
         _customer.addRentedItem(item.getID());
      }
   }
   public void processPayment(double amount)
   {
	   choosePaymentType();
	   paymentType.makePayment(amount);
   }
   public abstract double getPayment(Item item);
   
   public double calculatePayment(double price)
   {
      return _membership.calculatePayment(price);
   }
   
   public void addTax()
   {
      totalPayment = totalPayment * (1 + taxRate);
   }
   
   public void choosePaymentType()
   {
      System.out.println("Choose a Payment Type: \n" +
      		"\t1. Cash\n" +
      		"\t2. Check\n" +
      		"\t3. CreditCard\n");
      Scanner in = new Scanner(System.in);
      int choice = in.nextInt();
      
      if(choice == 1)
         paymentType = new Cash();
      else if(choice == 2)
         paymentType = new Check();
      else if(choice == 3)
         paymentType = new CreditCard();
      else
      {
         System.out.println("Invalid option. Try again.");
         choosePaymentType();
      }
   }
   
   public void makePayment()
   {
      paymentType.makePayment(totalPayment);
   }  
   
   public void setTaxRate(double tax)
   {
      taxRate = tax;
   }
   
   public double getTaxRate()
   {
      return taxRate;
   }
}
